//
//  PathParser.m
//  AutoCompiler_Script
//
//  Created by Vescky on 13-5-16.
//  Copyright (c) 2013年 Vescky. All rights reserved.
//

#import "PathParser.h"

@implementation PathParser

@synthesize templateProjectName,latestSdkVersion,defaultAndroidProjectPath,defaultIosProjectPath,defaultTemplatePath,defaultAndroidLibsPath,deploymentSdkVersion,appBundleIdHeadString,workSpaceFolderPath,outAndroidProjectPath,outIosProjectPath,outputAppPath,outputLogFilesPath,incomeBundleName,incomeBundlePath,scriptFilesPath,merchantAppsPath,wipServerIP,prodServerIP,wipXmppIP,prodXmppIP,testProdServerIP,testProdXmppIP,testWipServerIP,testWipXmppIP;

static PathParser *autopp;
static int mode;
static NSString *runPlatform;//dataInfo中的参数

//+ (id)initPathParserWithMode:(int)_mode {
//    mode = _mode;
//    if (!autopp) {
//        autopp = [[PathParser alloc] init];
//        [autopp parseForPath];
//    }
//    return autopp;
//}

#pragma mark - 
//若无设置，返回默认值
+ (id)sharePathParser {
    if (!autopp) {
        autopp = [[PathParser alloc] init];
        if (!mode) {
            mode = 0;
        }
        if (!runPlatform) {
            runPlatform = @"MP1-CN";
        }
        [autopp parseForPath];
    }
    return autopp;
}

+ (id)initPathParserWithWithMode:(int)_mode runPlatform:(NSString*)_platform {//workFolder:(NSString*)workFolder 
    if (_mode != mode || ![_platform isEqualToString:runPlatform]) {
        runPlatform = _platform;
        mode = _mode;
        if (!autopp) {
            autopp = [[PathParser alloc] init];
        }
        [autopp parseForPath];
    }
    
    return autopp;
}

- (void)saveChanges:(PathParser*)pp {
    autopp.templateProjectName = [pp.templateProjectName copy];
    autopp.latestSdkVersion = [pp.latestSdkVersion copy];
    autopp.defaultAndroidProjectPath = [pp.defaultAndroidProjectPath copy];
    autopp.defaultIosProjectPath = [pp.defaultIosProjectPath copy];
    autopp.deploymentSdkVersion = [pp.deploymentSdkVersion copy];
    autopp.appBundleIdHeadString = [pp.appBundleIdHeadString copy];
    autopp.workSpaceFolderPath = [pp.workSpaceFolderPath copy];
    autopp.outAndroidProjectPath = [pp.outAndroidProjectPath copy];
    autopp.outIosProjectPath = [pp.outIosProjectPath copy];
    autopp.outputAppPath = [pp.outputAppPath copy];
    autopp.outputLogFilesPath = [pp.outputLogFilesPath copy];
    autopp.incomeBundleName = [pp.incomeBundleName copy];
    autopp.incomeBundlePath = [pp.incomeBundlePath copy];
    autopp.scriptFilesPath = [pp.scriptFilesPath copy];
    autopp.merchantAppsPath = [pp.merchantAppsPath copy];
    autopp.wipServerIP = [pp.wipServerIP copy];
    autopp.prodServerIP = [pp.prodServerIP copy];
    autopp.defaultTemplatePath = [pp.defaultTemplatePath copy];
    autopp.defaultAndroidLibsPath = [pp.defaultAndroidLibsPath copy];
    autopp.wipXmppIP = [pp.wipXmppIP copy];
    autopp.prodXmppIP = [pp.prodXmppIP copy];
}


//- (void)changeWorkSpacePath:(NSString*)wPath {
//    if (!wPath) {
//        return;
//    }
//    
//    self.workSpaceFolderPath = wPath;
//    
//    if (self.defaultTemplatePath && self.defaultTemplatePath.length > 1) {
//        self.defaultIosProjectPath = [NSString stringWithFormat:@"%@/ios/%@",self.defaultTemplatePath,self.templateProjectName];
//        
//        self.defaultAndroidProjectPath = [NSString stringWithFormat:@"%@/android/%@",self.defaultTemplatePath,self.templateProjectName];
//    }
//    else {
//        self.defaultIosProjectPath = [NSString stringWithFormat:@"%@/defaultTemplate/ios/%@",self.workSpaceFolderPath,self.templateProjectName];
//    
//        self.defaultAndroidProjectPath = [NSString stringWithFormat:@"%@/defaultTemplate/android/%@",self.workSpaceFolderPath,self.templateProjectName];
//    }
//    
//    //设置 android libs 的路径
//    if (!self.defaultAndroidLibsPath || self.defaultAndroidLibsPath.length < 3) {
//        self.defaultAndroidLibsPath = [NSString stringWithFormat:@"%@/libs",[self.defaultAndroidProjectPath stringByDeletingLastPathComponent]];
//        NSFileManager *fm = [NSFileManager defaultManager];
//        if (![fm fileExistsAtPath:self.defaultAndroidLibsPath]) {
//            self.defaultAndroidLibsPath = [self.defaultAndroidLibsPath stringByDeletingLastPathComponent];
//        }
//    }
//    
//    self.incomeBundlePath = [NSString stringWithFormat:@"%@/tmp/%@",self.workSpaceFolderPath,self.incomeBundleName];
//    
//    self.outIosProjectPath = [NSString stringWithFormat:@"%@/out/ios/%@",self.workSpaceFolderPath,self.templateProjectName];
//    self.outAndroidProjectPath = [NSString stringWithFormat:@"%@/out/android/%@",self.workSpaceFolderPath,self.templateProjectName];
//    
//    self.outputAppPath = [NSString stringWithFormat:@"%@/products",self.workSpaceFolderPath];
//    self.outputLogFilesPath = [NSString stringWithFormat:@"%@/logs",self.workSpaceFolderPath];
//    self.scriptFilesPath = [NSString stringWithFormat:@"%@/tools/workShells",self.workSpaceFolderPath];
//    self.merchantAppsPath = [NSString stringWithFormat:@"%@/MerchantApps",self.workSpaceFolderPath];
//}

- (void)changeWorkSpacePath:(NSString*)wPath {
    
    //设置大本营目录
    if (self.defaultTemplatePath && self.defaultTemplatePath.length > 1) {
        self.defaultIosProjectPath = [NSString stringWithFormat:@"%@/ios/%@",self.defaultTemplatePath,self.templateProjectName];
        
        self.defaultAndroidProjectPath = [NSString stringWithFormat:@"%@/android/%@",self.defaultTemplatePath,self.templateProjectName];
    }
    else {
        self.defaultIosProjectPath = [NSString stringWithFormat:@"%@/defaultTemplate/ios/%@",self.workSpaceFolderPath,self.templateProjectName];
        
        self.defaultAndroidProjectPath = [NSString stringWithFormat:@"%@/defaultTemplate/android/%@",self.workSpaceFolderPath,self.templateProjectName];
    }
    
    //设置 android libs 的路径
    if (!self.defaultAndroidLibsPath || self.defaultAndroidLibsPath.length < 3) {
        self.defaultAndroidLibsPath = [NSString stringWithFormat:@"%@/libs",[self.defaultAndroidProjectPath stringByDeletingLastPathComponent]];
        NSFileManager *fm = [NSFileManager defaultManager];
        if (![fm fileExistsAtPath:self.defaultAndroidLibsPath]) {
            self.defaultAndroidLibsPath = [self.defaultAndroidLibsPath stringByDeletingLastPathComponent];
        }
    }
    
    self.scriptFilesPath = [NSString stringWithFormat:@"%@/tools/workShells",self.workSpaceFolderPath];
    self.merchantAppsPath = [NSString stringWithFormat:@"%@/MerchantApps",self.workSpaceFolderPath];
    
//    if (!wPath) {
//        return;
//    }
//    
//    //设置tmp目录
//    self.incomeBundlePath = [NSString stringWithFormat:@"%@/tmp/%@",wPath,self.incomeBundleName];
//    self.outIosProjectPath = [NSString stringWithFormat:@"%@/out/ios/%@",wPath,self.templateProjectName];
//    self.outAndroidProjectPath = [NSString stringWithFormat:@"%@/out/android/%@",wPath,self.templateProjectName];
//    
//    self.outputAppPath = [NSString stringWithFormat:@"%@/products",wPath];
//    self.outputLogFilesPath = [NSString stringWithFormat:@"%@/logs",wPath];
    
}


- (void)parseForPath {
    NSString *configPath = [self getConfigFilePath];
    NSString *configContent = [NSString stringWithContentsOfFile:configPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [configContent componentsSeparatedByString:@"\n"];
    if ([arr count] <= 0) {
        return;
    }
    for (int i = 0; i < [arr count]; i++) {
        NSString *str = [arr objectAtIndex:i];
        if (str.length > 0) {
            NSArray *arr1 = [str componentsSeparatedByString:@"="];
            if ([arr1 count] == 2) {
                NSString *str1 = [arr1 objectAtIndex:0];
                if ([str1 isEqualToString:@"template_project_name"]) {
                    self.templateProjectName = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"latest_sdk_version"]) {
                    self.latestSdkVersion = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"deploy_sdk_version"]) {
                    self.deploymentSdkVersion = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"work_folder"]) {
                    self.workSpaceFolderPath = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"app_bundleid_head"]) {
                    self.appBundleIdHeadString = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"app_income_bundle_name"]) {
                    self.incomeBundleName = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"wip_server_ip"]) {
                    self.wipServerIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"prod_server_ip"]) {
                    self.prodServerIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"default_template_folder"]) {
                    self.defaultTemplatePath = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"default_android_libs_folder"]) {
                    self.defaultAndroidLibsPath = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"wip_xmpp_ip"]) {
                    self.wipXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"prod_xmpp_ip"]) {
                    self.prodXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_wip_xmpp_ip"]) {
                    self.testWipXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_prod_xmpp_ip"]) {
                    self.testProdXmppIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_wip_server_ip"]) {
                    self.testWipServerIP = [arr1 objectAtIndex:1];
                }
                else if ([str1 isEqualToString:@"test_prod_server_ip"]) {
                    self.testProdServerIP = [arr1 objectAtIndex:1];
                }
                
            }
        }
    }
    
    [self changeWorkSpacePath:self.workSpaceFolderPath];
    
}

- (NSString*)getConfigFilePath {
    //优先查找该类型的配置，若无则用默认配置
    NSString *configDirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"config"];
    NSString *configName = [NSString stringWithFormat:@"%@_%d.config",runPlatform,mode];
    NSString *configPath = [configDirPath stringByAppendingPathComponent:configName];
    NSFileManager *fm = [NSFileManager defaultManager];
    
    //自动读取默认OEM的配置;0是默认OEM的配置文件;
    if (![fm fileExistsAtPath:configPath isDirectory:NO]) {
        NSLog(@"^^^^^^^^^ config not exist:%@ ^^^^^^^^^^^^",configPath);
        if (runPlatform && runPlatform.length > 0) {
            configName = [NSString stringWithFormat:@"%@_0.config",runPlatform];
            configPath = [configDirPath stringByAppendingPathComponent:configName];
        }
    }
    
    //OEM默认配置文件不存在,用default.config
    if (![fm fileExistsAtPath:configPath isDirectory:NO]) {
        NSLog(@"^^^^^^^^^ config not exist:%@ ^^^^^^^^^^^^",configPath);
        configPath = [configDirPath stringByAppendingPathComponent:@"default.config"];
    }
    
    NSLog(@"^^^^^^^^^ config path:%@ ^^^^^^^^^^^^",configPath);
    
    return configPath;
}

@end
